var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingMoreHidden: true,
    offset: 1,
    limit: 10,
    totalPages: 1,
    hideHeader: true,
    hideBottom: true,
    commentInfo: {},
    commentList: [],
    productId:'',
    selectLevel:'',
  },
  onLoad: function (options) {
    this.setData({
      productId:'50',
      // options.productId
    })
    this.getCommentList();
  },
  onShow: function () {

  },
  //加载
  onReachBottom: function () {
    let that = this;
    that.setData({
      offset: that.data.offset + 1,
      hideBottom: false
    });
    if (that.data.offset < that.data.totalPages || that.data.offset == that.data.totalPages) {
      this.getCommentList()
    }
    setTimeout(function () {
      that.setData({
        hideBottom: true
      })
    }, 1000);
  },
  //刷新
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      offset: 1
    });
    that.setData({
      hideHeader: false,
      loadingMoreHidden: true,
    })
    setTimeout(function () {
      var date = new Date();
      that.setData({
        offset: 1,
        hideHeader: true
      })
      that.getCommentList()
    }, 1000);
    wx.stopPullDownRefresh()
  },
  // 获取评论
  getCommentList: function () {
    let that = this;
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/shop/products/detail/comments',
      data: {
        productId: that.data.productId,
        level: that.data.selectLevel,
        offset: that.data.offset,
        limit: that.data.limit,
      }
    }).then((res) => {
      console.log(res);
      var tempArray = that.data.commentList;
      that.setData({
        'commentList': res.data.data.pageUtils.rows,
        'totalPages': res.data.data.pageUtils.total,
      });
      let commentList = [];
      if (that.data.offset == 1) {
        that.setData({
          'commentList': res.data.data.pageUtils.rows,
        })
      } else {
        tempArray = tempArray.concat(res.data.data.pageUtils.rows);
        that.setData({
          loadingMoreHidden: true,
          commentList: tempArray,
        });
      };
      tempArray = [];
      if (res.data.data.pageUtils.rows.length != that.data.limit) {
        that.setData({
          loadingMoreHidden: false,
        });
      };
      that.setData({
        commentInfo: res.data.data
      });
    })
  },
  selectLevelFunction:function(e){
    let that = this;
    let level = e.currentTarget.dataset.level;
    that.setData({
      selectLevel:level,
      offset:1,
    });
    that.getCommentList();
  },
})